<%--
  Created by IntelliJ IDEA.
  User: 郭鸿宇
  Date: 2025/5/14
  Time: 15:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="/static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
<form class="layui-form layui-form-pane" action="" lay-filter="formFile">

    <div class="layui-form-item">
        <label class="layui-form-label">商铺id</label>
        <div class="layui-input-inline">
            <select name="storeId" id="storeId">
                <option value="">请选择商铺</option>
                <option value="1">C-0001</option>
                <option value="2">C-0001</option>
                <option value="3">C-0003</option>
            </select>
        </div>
    </div>

    <input type="text" name="imgUrl" id="imgUrl">
    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
        <i class="layui-icon layui-icon-upload"></i> 单图片上传
    </button>

    <div class="layui-form-item">
        <label class="layui-form-label">业主姓名</label>
        <div class="layui-input-block">
            <input type="text" name="ownerName" autocomplete="off" placeholder="请输入业主姓名" lay-verify="required" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">业主手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="ownerPhone" autocomplete="off" placeholder="请输入业主手机号码" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="demo2">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="/static/js/jquery-1.8.3.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;
        var $ = layui.$;
        var laydate = layui.laydate;
        var util = layui.util;

        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '/file/uploadFile', // 实际使用时改成您自己的上传接口即可。
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                // 若上传失败
                if(res.code != 200){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                //把上传后的图片路径给上面文本框回显,目的提交保存路径到数据库
                $("#imgUrl").val(res.data)

                // …
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function(n, elem, e){
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        // 提交事件
        form.on('submit(demo2)', function(data){
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            layer.alert(JSON.stringify(field), {
                title: '当前填写的字段值'
            });
            // 此处可执行 Ajax 等操作
            // …
            $.ajax({
                url:"/owner/add",
                type:"post",
                contentType:"application/json",
                data:JSON.stringify(field),
                dataType:"json",
                success(res){
                    if(res.code==200){
                        alert(res.msg)
                        location.href="/store/toList"
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        });

        getStoreList();

        function getStoreList(){
            $.ajax({
                url:"/store/selectStoreList",
                type:"post",
                dataType:"json",
                success(res){
                    console.log(res.msg)
                    if(res.code==200){
                        // $("#storeId").empty()
                        // $("#storeId").append(`
                        //     <option value="">请选择商铺id</option>
                        // `)
                        for (let store of res.data) {
                            $("#storeId").append(`
                            <option value="\${store.storeId}">\${store.storeNum}</option>
                        `)
                        }
                        form.render('select','formFile')
                    }
                }
            })
        }

    });
</script>

</body>
</html>